package com.msh.business.msgHandler;

import com.google.common.base.Strings;
import com.msh.annotations.ProcessType;
import com.msh.business.MsgHandler;
import com.msh.common.VMSystem;
import com.msh.contract.TaskCompleteContract;
import com.msh.service.VendingMachineService;
import com.msh.utils.JsonUtil;
import com.msh.viewmodel.VMDistance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 *
 * @author :zhangchuanfu
 * @Date :2023/1/9
 * @Time :21:40
 * @description :工单完成处理
 */
@Component
@ProcessType("taskCompleted")
public class TaskCompletedMsgHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vmService;//售货机服务

    @Override
    public void process(String jsonMsg) throws IOException {
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg,TaskCompleteContract.class);
        if (taskCompleteContract == null || Strings.isNullOrEmpty(taskCompleteContract.getInnerCode())) return;

        //如果是投放工单，将售货机修改为运营状态
        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_DEPLOY) {
            vmService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_RUNNING);
            //保存设备的坐标(数据库+es)
            var vmDistance = new VMDistance();
            vmDistance.setInnerCode(taskCompleteContract.getInnerCode());
            vmDistance.setLat(taskCompleteContract.getLat());
            vmDistance.setLon(taskCompleteContract.getLon());
            vmService.setVMDistance(vmDistance);
        }

        //如果是撤机工单，将售货机修改为撤机状态
        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
            vmService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_REVOKE);
        }
    }
}
